﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;
using LaunchPad.Core.Dialogs;

namespace LaunchPad.Core.Controls
{
    /// <summary>
    /// Interaction logic for LibraryControl.xaml
    /// </summary>
    public partial class LibraryControl : UserControl
    {
        private Point _mouseStartPoint;

        public LibraryControl()
        {
            InitializeComponent();
        }

        private void PropertyButtonClick(object sender, RoutedEventArgs e)
        {
            var productInfo = ((FrameworkElement)sender).DataContext as ProductInfo;

            if (productInfo != null)
            {
                ShowProperties(productInfo.Product);
            }
        }

        private void ProductDataGridMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 1)
            {
                _mouseStartPoint = Mouse.GetPosition(null);
                e.Handled = true;
            }
        }

        private void ProductListViewMouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed && productDataGrid.SelectedIndex != -1)
            {
                var productInfo = productDataGrid.SelectedItem as ProductInfo;

                if (productInfo != null)
                {
                    var product = productInfo.Product;

                    Point mousePos = e.GetPosition(null);
                    Vector diff = _mouseStartPoint - mousePos;

                    bool minDragMet =
                        Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
                        Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance;

                    if (e.LeftButton == MouseButtonState.Pressed && minDragMet)
                    {
                        // Do drag and drop asynchronously
                        Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                               (Action)delegate
                                                            {
                                                                var dragData = new DataObject("effectFormat",
                                                                                              product.CreateEffect());
                                                                DragDrop.DoDragDrop(this, dragData, DragDropEffects.Move);
                                                            });
                    }
                }
            }
        }

        private void AddProductButtonClick(object sender, RoutedEventArgs e)
        {
            var product = new Product();
            ShowProperties(product);
            if(!string.IsNullOrWhiteSpace(product.Name))
            {
                ProductFactory.Instance.AddProduct(product);
            }
        }

        private static void ShowProperties(Product product)
        {
            var dialog = new ProductDialog
            {
                DataContext = product,
                WindowStartupLocation = WindowStartupLocation.CenterScreen
            };

            dialog.ShowDialog();
        }
    }
}